<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\admin\Controller;

/**
 * 人脸识别
 * Class User
 * @package app\admin\controller
 */
class Face extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lg_face_recognition';

    /**
     * 人脸识别列表
     * @auth true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '人脸识别管理';
        //门店
        $distributor = $this->app->db->table('lg_distributor')->where('status=1')->order('id asc')->column('name','id');
        $this->assign('distributor', $distributor);
        //链接数据库
        //搜索
        $shop_id = input('shop_id');
        $sex = input('sex');
        $status = input('status');
        $age = input('age');
        $enter_time = input('enter_time');
        $leave_time = input('leave_time');
        $where = [];
        if ($shop_id) {
            $where[] = ['f.shop_id', '=', $shop_id];
        }
        if ($status) {
            $where[] = ['f.status', '=', $status];
        }
        if ($sex) {
            $where[] = ['f.sex', '=', $sex];
        }
        if ($age) {
            $where[] = ['f.age', '=', $age];
        }
        if ($enter_time) {
            $enter_time = explode(' - ', $enter_time);
            $where[] = ['f.enter_time', 'between time', $enter_time];
            $where[] = ['f.channel_id', '=', 0];
        }
        if ($leave_time) {
            $leave_time = explode(' - ', $leave_time);
            $where[] = ['f.enter_time', 'between time', $leave_time];
            $where[] = ['f.channel_id', '=', 1];
        }
        $query = $this->_query($this->table);
        $query->alias('f')->field('f.*,d.name,e.is_come');
        $query->join('lg_distributor d', 'f.shop_id  = d.id', 'left');
        $query->join('lg_equip e', 'e.equip_id= f.equipment_id and e.channel_id=f.channel_id');
        $query->where($where);
        $query->order('f.id desc')->page();
    }

    protected function _page_filter(&$list)
    {
        foreach ($list as $k => $v) {
            $list[$k]['sex'] = $v['sex'] == 1 ? '男' : '女';
        }
    }


    /**
     * 编辑备注
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function edit()
    {
        $face = $this->app->db->table('lg_face_recognition')->where('id=' . input('id'))->field('equipment_id,channel_id')->find();
        $is_come = $this->app->db->table('lg_equip')->where('equip_id="' . $face['equipment_id'] . '" and channel_id=' . $face['channel_id'])->field('is_come,shop_id')->find();
        $this->assign('is_come', $is_come);
        $this->shopName = $this->app->db->table('lg_distributor')->where('id', $is_come['shop_id'])->value('name', '');
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑结果处理
     * @param $result
     */
    protected function _edit_form_result($result)
    {
        if ($result) {
            $id = input('id', '');
            sysoplog('人脸识别', "修改id为：[{$id}]的备注成功");
        }
    }


    /**
     * 设置白名单
     * @auth true
     * @login true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_applyFormToken();
        $this->_save($this->table, $this->_vali([
            'status.in:0,1' => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }


    /**
     * 状态结果处理
     * @param boolean $result
     */
    protected function _state_save_result(bool $result)
    {
        if ($result) {
            [$id, $state] = [input('id'), input('status')];
            sysoplog('人脸识别管理', ($state ? '移出白名单' : '移入白名单') . "人脸识别白名单修改[{$id}]成功");
        }
    }

}
